/**
 * 自定义菜单的增删
 */
const https = require('https');

/**
 * post请求方式
 * @param {链接的主要地址} hostname 
 * @param {uri} path 
 * @param {详细的配置信息,是个字符串} content 
 * @returns 
 */
function postRequest(hostname,path,content){
    return new Promise((resolve,reject)=>{
        const options = {
            hostname:hostname,
            path:path,
            method:"POST",
            headers:{
                'Content-Type':'application/x-www-form-urlencoded',
                'Content-Length': Buffer.byteLength(content)
            }

        }
        const req = https.request(options,res=>{
            let data = "";
            res.setEncoding('utf-8');
            res.on('data',chunk=>data += chunk);
            res.on('end',()=>resolve(data))
        });
        req.on('error',err=>reject(err));
        req.write(content);
        req.end();
    });
}
//自定义菜单的创建
const {getAccessToken} = require('./getAccessToken');
function create_meun(){
    return new Promise(async(resolve,reject)=>{
        const access_token = await getAccessToken();
        const hostname = "api.weixin.qq.com";
        const path = `/cgi-bin/menu/create?access_token=${access_token}`;
        const menu = {
            "button":[
            {	
                 "name":"重要网站",
                 "sub_button":[
                     {
                        "type":"view",
                        "name":"bilibili官网",
                        "url":"https://www.bilibili.com/"
                     },
                     {
                        "type":"view",
                        "name":"我的网站",
                        "url":"https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx029c9b44dcf8867f&redirect_uri=http%3A%2F%2Fchentianyu.free.idcfengye.com%2Fwww&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"
                     }
                 ]
             },
             {
                  "name":"后端网站",
                  "sub_button":[
                  {	
                      "type":"view",
                      "name":"spring",
                      "url":"https://spring.io/"
                   },
                   {
                    "type":"view",
                    "name":"maven",
                    "url":"https://maven.apache.org/"
                    },
                    {
                        "type":"view",
                        "name":"nodejs",
                        "url":"https://nodejs.org/en/"
                    }
                ]
             },
             {
                 "name":"前端网站",
                 "sub_button":[
                     {
                        "type":"view",
                        "name":"VUE3",
                        "url":"https://v3.cn.vuejs.org/"
                     },
                     {
                        "type":"view",
                        "name":"UNI-APP",
                        "url":"https://uniapp.dcloud.io/"
                     },
                     {
                        "type":"view",
                        "name":"ElECTRON",
                        "url":"https://www.electronjs.org/"
                     }
                 ]
             }
            ]
        };
        try{
            // https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
            resolve(await postRequest(hostname,path,JSON.stringify(menu)));
        }catch(err){
            reject(err);
        }
    })
}

function getRequest(uri){
    return new Promise((resolve,reject)=>{
        
        //https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=ACCESS_TOKEN
        https.get(uri,res=>{
            let data = '';
            res.on('data',chunk=>data += chunk);
            res.on('end',()=>resolve(data));
            res.on('error',err=>reject(err));
        })
    });    
}

function delete_meun(){
    return new Promise(async (resolve,reject)=>{
        const access_token = await getAccessToken();
        let uri = await `https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=${access_token}`;
        try{
            resolve(await getRequest(uri));
        }catch(err){
            reject(err);
        }
    });    
}

module.exports = {
    create_meun,//添加菜单
    delete_meun,//删除菜单
    getRequest,//get请求
    postRequest//post请求
}